package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Admin;
import com.model.DoorDetail;
import com.model.DoorOrder;
import com.model.Employee;
import com.model.Orders;
import com.model.Product;
import com.service.EmployeeService;
import com.util.Conversion;
import com.util.JdbcUtil;




@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends BaseServlet {
	
	EmployeeService service = new EmployeeService();
	
	//检查如何登录
	public void toLogin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JdbcUtil jdbc = new JdbcUtil();
		String position=request.getParameter("position");
		String name= request.getParameter("name");
		String password= request.getParameter("password");
		System.out.println(password+"   "+name+"     "+position);
		String sql=null;
		if("员工".equals(position)){
			sql ="select * from employee where ename = ? and epassword= ?";
			List<Employee> es = (List) jdbc.queryPreparedStatement(sql, Employee.class, name,password);
			if(es.size()>0){
				Employee e = es.get(0);
				request.getSession().setAttribute("employee", e);
				response.sendRedirect("EmployeeIndex.jsp");
			}
		}else{
			sql ="select * from admin where aname = ? and apassword= ?";
			List<Admin> as = (List) jdbc.queryPreparedStatement(sql, Admin.class, name,password);
			if(as.size()>0){
				Admin a = as.get(0);
				request.getSession().setAttribute("admin", a);
				response.sendRedirect("backindex.jsp");
			}
		}
	}
	
	
	//员工自己修改自己的基本信息
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException  {
		try {
			Employee  e = (Employee) request.getSession().getAttribute("employee");
			String ephone = request.getParameter("ephone");
			String eaddress = request.getParameter("eaddress");
			e =service.update(e,ephone,eaddress);
			request.getSession().setAttribute("employee", e);
			request.getRequestDispatcher("Employee.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			response.sendRedirect("error.jsp");
		}
	}
	
	
	//查询预约的订单信息
	public void query(HttpServletRequest request, HttpServletResponse response) {
		try {
			String uname = request.getParameter("uname");
			Employee  e = (Employee) request.getSession().getAttribute("employee");
			List<DoorOrder> doors = service.query(e.getEid(),uname);
			request.setAttribute("doors",  doors);
			request.getRequestDispatcher("EmployeePendOrder.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	//修改密码
	public void  changepass(HttpServletRequest request, HttpServletResponse response) {
		
		try {
			Employee e = (Employee)request.getSession().getAttribute("employee");
			Integer eid = e.getEid();
			String oldpass = request.getParameter("oldpass");
			String newpass =  request.getParameter("newpass");
			String ensure =  request.getParameter("ensure");
			if(e.getEpassword().equals(oldpass) && newpass.equals(ensure)){
				service.changepass(eid, oldpass,newpass);
			}else{
				return;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//查询预约的订单信息
		public void queryDetail(HttpServletRequest request, HttpServletResponse response) {
			try {
				String oid = request.getParameter("oid");
				Employee  e = (Employee) request.getSession().getAttribute("employee");
				List<DoorDetail> details = service.queryDetail(e.getEid(),oid);
				request.setAttribute("details",  details);
				request.getRequestDispatcher("EmployeeOrderDetail.jsp?oid="+oid).forward(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	//删除
	public void del(HttpServletRequest request, HttpServletResponse response) {
		try {
			String oid = request.getParameter("oid");
			Double tprice =Double.parseDouble(request.getParameter("tprice")) ;
			Integer pid = Integer.parseInt(request.getParameter("pid"));
			service.del(oid, pid,tprice);
			request.getRequestDispatcher("EmployeeServlet?action=queryDetail&oid="+oid).forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
		
	//保存订单详细信息
	public void save(HttpServletRequest request, HttpServletResponse response){
		try {
			String oid = request.getParameter("oid");
			Integer pid = Integer.parseInt(request.getParameter("pid"));
			Double weight = Double.parseDouble(request.getParameter("weight"));
			Double tprice = Double.parseDouble(request.getParameter("tprice"));
			Double oldtprice = Double.parseDouble(request.getParameter("oldtprice"));
			System.out.println("oldtprice:"+"      "+oldtprice);
			System.out.println("oid:"+"      "+oid);
			System.out.println("tprice:"+"      "+tprice);
			service.save(oid, pid,weight,tprice,oldtprice);
			response.getWriter().write("1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
	//添加
	public void add(HttpServletRequest request, HttpServletResponse response){
		try {
			DoorDetail detail = new DoorDetail();
			Conversion.convert(detail, request);
			Employee  e = (Employee) request.getSession().getAttribute("employee");
			service.add(detail,e.getEid());
			request.getRequestDispatcher("EmployeeServlet?action=queryDetail&oid="+detail.getOid()).forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
	//从数据库中查询所有的produnct数据
	public void toaddView(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<Product> products = service.allproducts();
		String oid = request.getParameter("oid");
		Orders order = service.order(oid);
		request.setAttribute("order", order);
		request.setAttribute("products", products);
		request.getRequestDispatcher("EmployeeAddOrderDetail.jsp").forward(request, response);
	}
		
	//添加种类从数据库查询某类的价格并返回	
	public void returnPrice(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String pname = request.getParameter("pname");
		Double price = service.returnPrice(pname);
		response.getWriter().print(price);
	}
	
		
		
		
		
		
		
	
}
